/*
	Author: Raffia
	Email : zhaody901@126.com
	Date  : 2012-9-11
	@code : $('#scrollFollow').cScrollFollow();
*/
(function($){
	$.fn.cScrollFollow = function(options){
		var defaults = {
			scrollTime:800,
			debug:false
		};
		var opts = $.extend(defaults, options);

		this.each(function(i,ele){
			var $this = $(ele);
			if ((opts.offsetTop && opts.offsetBottom) || (!opts.offsetTop && !opts.offsetBottom)) {
				$this.html('Invalid Data');
			}

			if (opts.offsetTop){
				$this.css('top', opts.offsetTop);
				$(window).scroll(function(){
					$this.stop().animate({'top':opts.offsetTop+$(window).scrollTop()}, opts.scrollTime);
				});
			}
			if (opts.offsetBottom) {
				var ch = window.innerHeight;
				$(window).scroll(function(){
					var top = ch - ($this.outerHeight() + opts.offsetBottom);
					$this.stop().animate({'top':$(window).scrollTop()+top}, opts.scrollTime);
				});
			}
		});
		function debug(info) {
			if (opts.debug && typeof(window.console) != undefined) {
				console.log(info);
			} else if (opts.debug && typeof(window.console) == undefined) {
				alert(info);
			}
		}
	};
})(jQuery);